import check from './check.js'
import $http from './http'
// 图片上传OSS
function uploadImgOSS(imgUrl, type) {
	let params = { bizType: type }
	let upImageSrc = imgUrl
	let filename = 'fire-img' + check.guid()
	return new Promise(function(resolve, reject) {
		$http
			.post({
				url: '/ali/signature',
				data: params
			})
			.then(res => {
				uni.uploadFile({
					url: res.data.host,
					filePath: upImageSrc,
					fileType: 'image',
					name: 'file',
					formData: {
						name: 'test',
						'key': res.data.dir + filename + '.png',
						'policy': res.data.policy,
						'OSSAccessKeyId': res.data.accessid,
						'success_action_status': '200', //让服务端返回200,不然，默认会返回204
						'signature': res.data.signature,
					},
					success: resOss => {
						// console.log('resOss:', resOss)
						if (resOss.statusCode === 200) {
							let target = res.data.host + '/' + res.data.dir + filename + '.png'
							resolve(target)
						} else {
							//上传失败	
							that.$showToast('上传失败，请重新上传!', 'none')
						}
					},
					fail: err => {
						that.$showToast(err.errMsg, 'none')
					}
				})
			})
	})
}
// 人脸识别上传照片
function uploadImgOSSBase64(resData, type) {
	let params = { 'bizType': type }
	let upImageSrc = resData
	let filename = 'fire' + check.guid()
	return new Promise(function(resolve, reject) {
		$http
			.post({
				url: '/ali/signature',
				data: params
			})
			.then(res => {
				uni.uploadFile({
					url: res.data.host,
					filePath: upImageSrc,
					fileType: 'image',
					name: 'file',
					formData: {
						name: 'test',
						'key': res.data.dir + filename + '.png',
						'policy': res.data.policy,
						'OSSAccessKeyId': res.data.accessid,
						'success_action_status': '200', //让服务端返回200,不然，默认会返回204
						'signature': res.data.signature,
					},
					success: resOss => {
						// console.log('uploadImage success, resOss is:', resOss)
						if (resOss.statusCode === 200) {
							let target = res.data.host + '/' + res.data.dir + filename + '.png'
							resolve(target)
						} else {
							//上传失败	
							that.$showToast('上传失败，请重新上传!', 'none')
						}
					},
					fail: err => {
						that.$showToast(err.errMsg, 'none')
					}
				})
			})
	})
}

export default {
	async uploadImgOSS(imgRes, type) {
		return await uploadImgOSS(imgRes, type)
	},
	async uploadImgOSSBase64(imgRes, type) {
		return await uploadImgOSSBase64(imgRes, type)
	}
}
